package com.fjec.base.config;

import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fjec.common.aliyun.api.BankCardClient;
import com.fjec.common.aliyun.api.BaseAliyunApi;
import com.fjec.common.aliyun.api.BusinessLicenseClient;
import com.fjec.common.aliyun.api.IdCardClient;
import com.fjec.common.aliyun.sms.AliSmsConfig;
import com.fjec.common.aliyun.sms.AliyunSmsHelper;
import com.fjec.common.config.OSSConfig;
import com.fjec.common.config.CommonWebMvcConfig;
import com.fjec.common.okhttp.OkHttp;
import com.fjec.common.util.AliOSSUtil;
import com.fjec.common.util.SequenceIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import java.io.IOException;

@Configuration
public class AppConfig {

    @Value("${aliyun.api.accessKeyId}")
    private String aliApiAccessKeyId = "203856687";

    @Value("${aliyun.api.secret}")
    private String aliApiSecret = "nvipMZfZ5fHnGmfFh5vBxGqfalzxvt4r";

    @Bean(name = "storeAliOSSUtil")
    public AliOSSUtil storeAliOSSUtil(OSSConfig storeAliOSSConfig) {
        AliOSSUtil storeAliOSSUtil = new AliOSSUtil(storeAliOSSConfig.getAccessKeyId(),
                storeAliOSSConfig.getAccessKeySecret(), storeAliOSSConfig.getEndPoint(),
                storeAliOSSConfig.getBucket(),storeAliOSSConfig.getBucketName(),
                "", storeAliOSSConfig.getBindDomain());
        return storeAliOSSUtil;
    }

    @Bean(name = "storeAliOSSConfig")
    @ConfigurationProperties(prefix = "oss.config")
    public OSSConfig storeOSSConfig(){
        return new OSSConfig();
    }


    @Bean(name = "secretAliOSSUtil")
    public AliOSSUtil secretAliOSSUtil(OSSConfig secretOSSConfig) {
        AliOSSUtil secretAliOSSUtil = new AliOSSUtil(secretOSSConfig.getAccessKeyId(),
                secretOSSConfig.getAccessKeySecret(), secretOSSConfig.getEndPoint(),
                secretOSSConfig.getBucket(),secretOSSConfig.getBucketName(),
                "", secretOSSConfig.getBindDomain());
        return secretAliOSSUtil;
    }

    @Bean(name = "secretOSSConfig")
    @ConfigurationProperties(prefix = "oss.secret.config")
    public OSSConfig secretOSSConfig(){
        return new OSSConfig();
    }

    /**
     * 使用Apache-FileUpload组件上传
     */
    @Bean(name = "multipartResolver")
    public CommonsMultipartResolver multipartResolver() throws IOException {
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        multipartResolver.setMaxUploadSize(1024 * 1024 * 50);
        multipartResolver.setDefaultEncoding("utf-8");
        multipartResolver.setUploadTempDir(new FileSystemResource("./uploadTempDir"));
        multipartResolver.setMaxInMemorySize(10240);
        return multipartResolver;
    }

    @Bean("okHttp")
    public OkHttp okHttp() {
        OkHttp okHttp = new OkHttp();
        okHttp.init();
        return okHttp;
    }

    @Bean
    public IdCardClient idCardClient() {
        HttpClientBuilderParams params = new HttpClientBuilderParams();
        params.setAppKey(aliApiAccessKeyId);
        params.setAppSecret(aliApiSecret);
        IdCardClient client = new IdCardClient(params);
        return client;
    }

    @Bean
    public BusinessLicenseClient businessLicenseClient() {
        HttpClientBuilderParams params = new HttpClientBuilderParams();
        params.setAppKey(aliApiAccessKeyId);
        params.setAppSecret(aliApiSecret);
        BusinessLicenseClient client = new BusinessLicenseClient(params);
        return client;
    }

    @Bean
    public BankCardClient bankCardClient() {
        HttpClientBuilderParams params = new HttpClientBuilderParams();
        params.setAppKey(aliApiAccessKeyId);
        params.setAppSecret(aliApiSecret);
        BankCardClient client = new BankCardClient(params);
        return client;
    }

    @Bean
    @ConfigurationProperties(prefix = "base.sms.config")
    public AliSmsConfig aliSmsConfig() {
        return new AliSmsConfig();
    }

    @Bean
    public AliyunSmsHelper aliyunSmsHelper(AliSmsConfig aliSmsConfig) {
        return new AliyunSmsHelper(aliSmsConfig);
    }

}
